<template>
    <view class="container">
        <view class="info">
            <text>姓名：{{ userInfo.name }}</text>
            <text>年龄：{{ userInfo.age }}</text>
        </view>
        <scroll-view scroll-y ref="scroll">
			<view v-for="item in 50">{{ item }}</view>
		</scroll-view>
		<transition name="fade">
			<view class="fixed" v-show="showBackTop" @click="scrollToTop">
				🔼
			</view>
		</transition>
    </view>
</template>

<script setup>
import { ref } from 'vue'
import { onLoad, onReady, onShow, onHide, onUnload, onPageScroll } from '@dcloudio/uni-app'

const userInfo = ref({
    name: '',
    age: 0
})
const scroll = ref(null)
const showBackTop = ref(false)

onLoad((options) => {
    if (options.params) {
        // 解析传递的参数
        const params = JSON.parse(decodeURIComponent(options.params))
        userInfo.value = params
        console.log(params)
    }
    console.log('onLoad函数')
    console.log(scroll.value)
})

onReady(()=> {
    console.log('onReady函数')
	console.log(scroll.value)
})
// 进入页面
onShow(()=> {
    console.log('onShow函数')
})
// 离开页面
onHide(()=> {
    console.log('onHide函数')
})

onUnload(() => {
    console.log('onUnload函数')
})

onPageScroll((e) => {
    console.log(e)
    // 当滚动距离大于 200px 时显示按钮
    showBackTop.value = e.scrollTop > 200
})

const scrollToTop = () => {
    uni.pageScrollTo({
        scrollTop: 0,
        duration: 300  // 滚动动画时长，单位ms
    })
}

</script>

<style lang="scss" scoped>
.container {
    padding: 20px;
}

.info {
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: $uni-color-error;
}

.fixed {
	position: fixed;
	bottom: 20px;
	right: 20px;
    width: 50rpx;
    height: 50rpx;
    background-color: clear;
    border-radius: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
	font-size: 50rpx;
}

/* 淡入淡出动画 */
.fade-enter-active,
.fade-leave-active {
    transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
    opacity: 0;
}
</style>
